/*
 * Copyright (C) 2012 Atlas of Living Australia
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 */

package au.org.ala.phylojive

class UserService {

    def authService

    static transactional = true

    /**
     * Register the current user in the system.
     */
    def registerCurrentUser = {
        def userId = authService.getUserId()?.toLong()
        def email = authService.getEmail()
        def displayName = authService.getDisplayName()
        log.info("Checking user is registered: " + userId + " => " + displayName)

        if (userId) {
            // check if user is in app DB, if not create it on the fly
            if (User.findByUserId(userId) == null) {
                User user = new User()
                user.userId = userId
                user.created = new Date()
                user.email = email
                user.displayName = displayName
                user.save(flush: true)
            }
        }
    }
}

